package com.exam.utils;

import cn.hutool.core.util.StrUtil;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils extends StrUtil {

    /**
     * 判断字符串中是否有中文
     * @param content
     * @return
     */
    public static boolean isContainChinese(String content){
        if(isEmpty(content)){
            return false;
        }
        Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher m = p.matcher(content);
        return m.find();
    }

    /**
     * 字符串转首字母大写
     * @param content
     * @return
     */
    public static String distinctStr(String content){
        char[] chars = content.trim().toCharArray();
        if(chars[0]>='a' && chars[0]<='z'){
            chars[0]-=32;
        }
        return new String(chars);
    }

    public static long convertStringToLong(String input) {
        try {
            // 使用MD5算法对字符串进行哈希
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            // 将哈希结果转换为BigInteger
            BigInteger bigInt = new BigInteger(1, messageDigest);
            // 将BigInteger转换为long类型
            long uniqueLong = bigInt.longValue();
            // 如果结果为负数，则取其绝对值
            if (uniqueLong < 0) {
                uniqueLong = Math.abs(uniqueLong);
            }
            return uniqueLong;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return -1;
        }
    }

}
